package com.ouwen.smartpay.service.chain.slots.gid;

import com.fm.module.dubbo.RequestContextFilter;
import com.ouwen.smartpay.openapi.order.ServiceOrder;
import com.ouwen.smartpay.openapi.result.ServiceResult;
import com.ouwen.smartpay.service.chain.context.Context;
import com.ouwen.smartpay.service.chain.slotchain.AbstractLinkedProcessorSlot;
import com.ouwen.smartpay.service.chain.spi.SpiOrder;

import static com.ouwen.smartpay.service.chain.spi.SpiOrder.HIGHEST_PRECEDENCE;

/**
 * @author <a href="http://youngitman.tech">青年IT男</a>
 * @version v1.0.0
 * @className GidSlot
 * @description Gid默认值设置
 * @JunitTest: {@link  }
 * @date 2020-04-26 10:11
 **/
@SpiOrder(HIGHEST_PRECEDENCE)
public class GidSlot extends AbstractLinkedProcessorSlot<ServiceOrder> {

    @Override
    public void entry(Context context, ServiceOrder order, Object... args) throws Throwable {

        ServiceResult result = (ServiceResult) args[0];

        //设置通用参数以及gid
        buildServiceResult(result, order, context);

        //触发下一个slot执行
        fireEntry(context, order, args);

    }

    @Override
    public void exit(Context context, Object... args) {

        //触发退出slot操作
        fireExit(context, args);

    }

    private void buildServiceResult(ServiceResult result, ServiceOrder order, Context context) {

        String gid = order.getGid();

        //初始化gid
        order.setGid(gid);

        result.setGid(gid);

        result.setPartnerId(order.getPartnerId());

        result.setRequestNo(order.getRequestNo());

        result.setService(order.getService());

        //上下文传递gid
        context.setGid(order.getGid());

        RequestContextFilter.RequestContext.getContext().setGid(order.getGid());

    }

}
